<?php
include $_SERVER['DOCUMENT_ROOT']. '/lib/Sanpham.php';
$sanphamOb = new Sanpham();
//Xu ly them xoa
$command = '';
$id = '';
$message = array();
if (isset($_GET['command'])) {
    $command = $_GET['command'];
}
if (isset($_GET['id'])) {
    $id = $_GET['id'];
}
if ($command == 'del') {
    if ($sanphamOb->xoaSanPham($id)) {
        $message[] = "Xóa thành công";
    } else {
        $message[] = "Xóa thất bại";
    }
}
$dsSanpham= $sanphamOb->layDanhSachSanPham();
//-------------------
$display = 4;
// tinh tong so trang can hien thi
if (isset($_GET['page']) && (int) $_GET['page']) {
    $page = $_GET['page'];
} else { //neu chua xac dinh, thi tim so trang
    $record = $sanphamOb->demSanPham();
    if ($record > $display) {
        $page = ceil($record / $display);
    } else {
        $page = 1;
    }
}
$start = (isset($_GET['start']) && (int) $_GET['start'] >= 0) ? $_GET['start'] : 0;

$dsSanpham = $sanphamOb->layDSSP($start, $display);
?>
<div class="form">
    <form method="post" action="sanpham.php?ava=list">
        <input name="txttimkiemsp" type="text" class="colortext"  value="Tên sản phẩm..." />
        <input type="submit" name="timkiem" value="GO">
    </form>
</div>
<link href="admin.css" rel="stylesheet">
<div style="margin: 5px;padding: 30px;">
    <h2 style="color: #0000cc" align="center">Danh sách sản phẩm</h2>
    <div class="messages">
        <?php foreach ($message as $mes): ?>
            <p><?php echo $mes; ?></p>
        <?php endforeach; ?>
    </div>
    <div class="command_area">
        <a href="/admin/capnhatsanpham.php">Thêm mới sản phẩm</a>
    </div>
    <table style="width: 100%">
        <thead>
            <tr>
                <th scope="col" align="left">ID</th>
                <th scope="col" align="left">Tên sản phẩm</th>
                <th scope="col" align="left">Thuộc Danh Mục</th>
                <th scope="col" align="left">Giá</th>
                <th scope="col" align="left">Mô tả</th>
                <th scope="col" align="left">Đặc biệt</th>
                <th scope="col" align="left">Hình ảnh</th>
                <th scope="col" align="left">Trạng thái</th>
                <th scope="col"></th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($dsSanpham as $sanpham): ?>
                <tr>
                    <td scope="col"><?php echo $sanpham['id'] ?></td>
                    <td scope="col"><?php echo $sanpham['ten'] ?></td>
                    <td scope="col"><?php echo $sanpham['tieude'] ?></td>
                    <td scope="col"><?php echo $sanpham['gia'] ?></td>
                    <td scope="col"><?php echo $sanpham['mota'] ?></td>
                    <td scope="col"><?php echo $sanpham['dacbiet'] ?></td>
                    <td scope="col"><img src="<?php echo $sanpham['hinhanh'] ?>" width="120px;"  /> </td>
                    <td scope="col"><?php echo $sanpham['trangthai'] ?></td>
                    <td scope="col"><?php echo "<a href='/admin/capnhatsanpham.php?id=" . $sanpham['id'] . "'>Sửa"; ?></td>
                    <td scope="col"><?php echo "<a href='/admin/sanpham.php?command=del&id=" . $sanpham['id'] . "'>Xóa"; ?></td>                   
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <th scope="row" colspan="11"><p style="color: #0099ff">.:Danh Mục Sản Phẩm:.</p></th>
        </tr>
        </tfoot>
    </table>
    </div>

     <div style="padding: 0 auto;" align="center"><?php
            if ($page > 1) { //neu can hien thi so trang
                $next = $start + $display;
                $prev = $start - $display;
                $current = ($start / $display) + 1;

                //Hien thi trang Previous
                if ($current != 1) {
                    echo "<a href='sanpham.php?start=$prev&page=$page'>Previous</a>";
                    echo ".";
                }
                //Hien thi so link
                for ($i = 1; $i <= $page; $i++) {
                    if ($current != $i) {
                        echo ".";
                        echo "<a href='sanpham.php?start=" . ($display * ($i - 1)) . "&page=$page'>$i</a>";
                    } else {
                        echo $i;
                    }
                } //End: FOR
                //Hien thi trang Next
                if ($current != $page) {
                    echo "..";
                    echo "<a href='sanpham.php?start=$next&page=$page'>Next</a>";
                }
            }//End: $page > 1 IF
            ?></div>
</div>